package com.sample.tdf.sample.api;

import cn.com.taiji.common.dto.PageDTO;
import cn.com.taiji.common.dto.ResultDTO;
import cn.com.taiji.common.exception.BaseException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.sample.tdf.sample.dto.CourseDTO;
import com.sample.tdf.sample.service.ICourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 太极计算机股份有限公司
 * @since 2020-04-07
 */
@Api(tags = "课程接口")
@RestController
@RequestMapping("/sample/course")
public class CourseController {
    @Autowired
    private ICourseService iCourseService;

    @Valid
    @ApiOperation("1.新增")
    @ApiOperationSupport(order = 1)
    @PostMapping
    public ResultDTO<CourseDTO> create(@RequestBody CourseDTO dto) throws BaseException {
        CourseDTO courseDTO = iCourseService.create(dto);
        return new ResultDTO(courseDTO);
    }

    @ApiOperation("2.批量新增")
    @ApiOperationSupport(order = 2)
    @PostMapping({"/batchCreate"})
    public ResultDTO batchCreate(@RequestBody List<CourseDTO> dtos) throws BaseException {
       List<CourseDTO> list = iCourseService.batchCreate(dtos);
        return new ResultDTO(list);
    }

    @Valid
    @ApiOperation("3.修改")
    @ApiOperationSupport(order = 3)
    @PutMapping
    public ResultDTO<String> update(@RequestBody CourseDTO dto) throws BaseException {
        iCourseService.update(dto);
        return new ResultDTO();
    }

    @ApiOperation("4.根据id删除")
    @ApiOperationSupport(
            order = 4
    )
    @DeleteMapping({"/{id}"})
    public ResultDTO<Boolean> deleteById(@PathVariable("id") String id) throws BaseException {
        boolean b = iCourseService.removeById(id);
        return new ResultDTO(b);
    }

    @ApiOperation("5.根据多个id批量删除")
    @ApiOperationSupport(
            order = 5
    )
    @DeleteMapping({"/deleteByIds"})
    public ResultDTO<Boolean> deleteByIds(@RequestBody List<String> ids) throws BaseException {
        boolean b = iCourseService.removeByIds(ids);
        return new ResultDTO(b);
    }

    /**
     * 一对一 使用association 进行二次查询
     * @param id
     * @return
     * @throws BaseException
     */
    @ApiOperation("6.一对一嵌套两次查询")
    @ApiOperationSupport(order = 6)
    @GetMapping({"/{id}"})
    public ResultDTO<CourseDTO> selectById(@PathVariable("id") String id) throws BaseException {
        CourseDTO courseDTO =   iCourseService.findById(id);
        return new ResultDTO(courseDTO);
    }

    /**
     * 一对一关心 使用association 进行二次查询
     * @param id
     * @return
     * @throws BaseException
     */
    @ApiOperation("6.一对一嵌套一次查询")
    @ApiOperationSupport(order = 6)
    @GetMapping({"/findByIdOneSelect"})
    public ResultDTO<CourseDTO> findByIdOneSelect(String id) throws BaseException {
        CourseDTO courseDTO =  iCourseService.findByIdOneSelect(id);
        return new ResultDTO(courseDTO);
    }

    @Valid
    @ApiOperation("7.普通单表分页查询")
    @ApiOperationSupport(order = 7)
    @PostMapping({"/search"})
    public ResultDTO<PageDTO<CourseDTO>> selectByPage(@RequestBody PageDTO<CourseDTO> pageDTO) {
        return new ResultDTO(pageDTO);
    }
}

